import { GTree } from 'geminis-react';
import { forwardRef, useCallback, useImperativeHandle, useRef, useState } from 'react';

export const CategoryTree = forwardRef((props: any, ref: any) => {
  const tree = useRef<any>();
  const [selection, setSelection] = useState<any>();

  useImperativeHandle(
    ref,
    () => {
      return {
        selection: selection,
        reloadSelectedNode: () => tree.current.reloadSelectedNode(),
      };
    },
    [selection],
  );

  const onSelected = useCallback((selection: any[]) => {
    setSelection(selection);
    props.onSelected?.(selection);
  }, []);

  return (
    <GTree
      ref={tree}
      url="/api/workflow/category/children/"
      root={props.showRoot ? { id: '', name: '流程类型', pathId: '/' } : null}
      {...props}
      onSelected={onSelected}
    />
  );
});
